<?php
if (@mysql_connect('localhost', 'root', ''))
	echo	"Connected to MySQL engine.</b><br/>";
else
	die('<b>Unable to connect to MySQL engine:</b> '.mysql_error());

$db_found = false;
$db_list = mysql_list_dbs();
while (!$db_found && $db = mysql_fetch_object($db_list))
	if ($db->Database == 'photosaic')
		$db_found = true;
if ($db_found)
	echo	"Photosaic MySQL Database already created.<br/>";
else
	if (mysql_query('create database photosaic'))
		echo	"Photosaic MySQL Database successfully created.<br/>";
	else
		die('<b>Failed creating Photosaic MySQL Database:</b> '.mysql_error());

mysql_select_db('photosaic');

if (mysql_num_rows(mysql_query("show tables like 'photo_cache'")))
	echo	"photo_cache table already exists.<br/>";
else {
	$query = "CREATE TABLE `photo_cache` (
  `photo_id` int(11) NOT NULL auto_increment,
  `url` varchar(128) collate latin1_general_ci NOT NULL,
  `r` int(2) NOT NULL,
  `g` int(2) NOT NULL,
  `b` int(2) NOT NULL,
  `r_match` float(5,2) NOT NULL,
  `g_match` float(5,2) NOT NULL,
  `b_match` float(5,2) NOT NULL,
  `rgb_match` float(5,2) NOT NULL,
  `added` datetime NOT NULL,
  `last_used` datetime NOT NULL,
  PRIMARY KEY  (`photo_id`),
  UNIQUE KEY `url` (`url`)
)";
	if (mysql_query($query))
		echo	"Successfully created photo_cache table.<br/>";
	else
		die("Failed creating photo_cache table: ".mysql_error());
}

if (mysql_num_rows(mysql_query("show tables like 'photo_cache2'")))
	echo	"photo_cache table already exists.<br/>";
else {
	$query = "CREATE TABLE `photo_cache2` (
  `photo_id` int(11) NOT NULL auto_increment,
  `url` varchar(128) collate latin1_general_ci NOT NULL,
  `r` int(2) NOT NULL,
  `g` int(2) NOT NULL,
  `b` int(2) NOT NULL,
  `last_used` datetime NOT NULL,
  PRIMARY KEY  (`photo_id`),
  UNIQUE KEY `url` (`url`)
)";
	if (mysql_query($query))
		echo	"Successfully created photo_cache2 table.<br/>";
	else
		die("Failed creating photo_cache table: ".mysql_error());
}
?>